import * as s from '@/components/utils/sessionStorage'
// import { asyncRouterMap } from '@/router'
const account = {
  state: {
    userInfo: s.get('userInfo') || {},
    userInfoDetail: s.get('userInfoDetail') || {},
    permission: s.get('permission') || [],
    permissionSecond: s.get('permissionSecond') || [],
  },
  mutations: {
    setMenu: state => {
      state.userInfo = s.get('userInfo') || {}
    },
    setUserInfoDetail: state => {
      state.userInfoDetail = s.get('userInfoDetail') || {}
    },
    setPermission: state => {
      state.permission = s.get('permission') || []
    },
    setPermissionSecond: state => {
      state.permissionSecond = s.get('permissionSecond') || []
    },
  },
  actions: {
    setMenu: ({ commit, dispatch, state }, payload) => {
      s.set('userInfo', payload)
      commit('setMenu')
      // 获取一级菜单栏
      if (JSON.stringify(payload) !== '{}') {
        let menuValue = payload.modules.CHILD
        let moduleId = ''
        let moduleFirst = []
        s.set('permission', menuValue)
        commit('setPermission')
      } else {
        s.set('permission', [])
        commit('setPermission')
      }
    },
    setPermission: ({ commit, dispatch, state }, payload) => {
      if (payload.length < 1) {
        s.set('permission', [])
        commit('setPermission')
      } else {
        // let arr = getPermission(payload)
        s.set('permission', payload)
        commit('setPermission')
      }
    },
    // 二级菜单栏
    setPermissionSecond: ({ commit, dispatch, state }, payload) => {
      if (payload.length < 1) {
        s.set('permissionSecond', [])
        commit('setPermissionSecond')
      } else {
        // let arr = getPermission(payload)
        s.set('permissionSecond', payload)
        commit('setPermissionSecond')
      }
    },
    logout: ({ commit, dispatch, state }, payload) => {
      dispatch('setMenu', {})
    },
    setUserInfoDetail: ({ commit, dispatch, state }, payload) => {
      s.set('userInfoDetail', payload)
      commit('setUserInfoDetail')
    }
  }
}

export default account

// const getPermission = (payload) => {
//   let newArr = JSON.parse(JSON.stringify(asyncRouterMap)) //数组深拷贝
//   newArr.forEach((item) => {
//     let arr = []
//     item.children.forEach(child => {
//       payload.forEach((el) => {
//         if (el === child.meta.title) {
//           arr.push(child)
//         }
//       })
//     })
//     item.children = arr
//   })
//   return newArr
// }